<template>
  <div class="CustomeRelationship">
    <el-form :inline="true" class="demo-form-inline">

      <div class="locationItem kehubianhao">

        <el-form-item label="客户编号">
          <el-input class="input-item" v-model="customer.code" disabled></el-input>
        </el-form-item>
        <el-form-item label="客户名称">
          <el-input class="input-item" v-model="customer.name" disabled></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 30px;" @click="">帮助</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 30px;" @click="onSubmit('新建')">新建</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 30px;" @click="$router.go(-1)">返回</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData.records" :highlight-current-row="true" style="max-height: 75%;width: 100%;">
      <el-table-column prop="id" :align="'center'" label="编号" width="120" />
      <el-table-column prop="relationTime" label="时间" width="180" />
      <el-table-column prop="address" :align="'center'" label="地点" />
      <el-table-column prop="summary" :align="'center'" label="概要" />
      <el-table-column prop="detail" :align="'center'" label="详细信息" />
      <el-table-column prop="content" :align="'center'" label="备注" />
      <el-table-column label="操作" :align="'center'">
        <template slot-scope="scope">
          <i class="el-icon-edit" @click="onSubmit('编辑',scope.$index, scope.row)"></i>
          <i class="el-icon-delete" @click="handleDelete(scope.$index, scope.row)"></i>
        </template>
      </el-table-column>
    </el-table>
    <div class="pager">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="current"
        :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total">
      </el-pagination>
    </div>
    <el-dialog :modal="true" :title="event+modName" :visible.sync="dialogFormVisible">
      <el-form :model="relationship" :rules="rules" ref="relationship" label-position="right" label-width="80px"
        :inline="true">
        </el-date-picker>
        <el-form-item label="时间" prop="relationTime">
          <el-input v-model="relationship.relationTime" type="date" placeholder="选择日期"></el-input>
        </el-form-item>
        <el-form-item label="地点" prop="address">
          <el-input v-model="relationship.address"></el-input>
        </el-form-item>
        </el-form-item>
        <el-form-item label="概要" prop="summary">
          <el-input v-model="relationship.summary"></el-input>
        </el-form-item>
        <el-form-item label="详细信息" prop="detail">
          <el-input v-model="relationship.detail"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="content">
          <el-input v-model="relationship.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'CustomerContact',
    data() {
      return {
        modName: '交往记录',
        event: '',
        currentIndex: '',
        currentRow: '',
        customer: {},
        relationship: {},
        tableData: [],
        current: 1,
        pageSize: 5,
        dialogFormVisible: false,
        rules: {
          relationTime: [{
            required: true, message: '请选择时间'
          }],
          address: [
            { required: true, message: '请输入地址' },
            { max: 20, message: '最多20个字符', trigger: 'blur' }
          ],
          summary: [
            { required: true, message: '请输入概要' },
            { max: 20, message: '最多20个字符', trigger: 'blur' }
          ],
          detail: [{
            max: 20, message: '最多20个字符', trigger: 'blur'
          }],
          content: [{
            max: 20, message: '最多20个字符', trigger: 'blur'
          }]
        }
      }
    }, methods: {
      getRouterData() {
        this.customer = JSON.parse(sessionStorage.getItem('customer'));
      }, handleSizeChange(val) {
        this.pageSize = val;
        this.getRelationshipList();
      }, handleCurrentChange(val) {
        this.current = val;
        this.getRelationshipList();
      }, onSubmit(event, index, row) {
        this.event = event;
        this.currentIndex = index;
        this.dialogFormVisible = true;
        this.relationship = event == '编辑' ? row : {};
      }, handleAdd() {
        this.relationship.customerId = this.customer.id;
        this.sendeRequest('post', '/customer/customerInfo/relationship/add', this.relationship);
      }, handleEdit(index, row) {
        let currentData = this.tableData.records[index];    //当前修改行数据
        currentData = this.relationship;
        this.sendeRequest('put', '/customer/customerInfo/relationship/edit', currentData);
      }, handleDelete(index, row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.tableData.records.splice(index, 1);
          this.current = --this.tableData.total <= (this.current-1)*this.pageSize ? --this.current : this.current;
          this.sendeRequest('delete', '/customer/customerInfo/relationship/del', { id: row.id });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }, confirm() {
        this.$refs['relationship'].validate((valid) => {
          if (valid) {
            switch (this.event) {
              case '新建':
                this.handleAdd();
                break;
              case '编辑':
                this.handleEdit();
                break;
            }
            console.log('--');
            this.getRelationshipList();
            this.cancel();
          } else {
            this.$message({
              message: '您还有未完善的选项',
              type: 'warning'
            })
            return false;
          }
        });
      }, cancel() {
        this.dialogFormVisible = false;
        this.relationship = {};
      }, getRelationshipList() {
        let _this = this;
        console.log('查询');
        _this.$http.get('/customer/customerInfo/relationship', {
          params: {
            pageIndex: _this.current,
            pageSize: _this.pageSize,
            customerId: _this.customer.id
          }
        }).then(function (ret) {
          _this.tableData = ret.data;
        });
      }, sendeRequest(method, url, param) {  //实现更新请求的代码复用
        let _this = this;
        param = method == 'delete' ? { params: param } : param;
        this.$http[method](url, param).then(function (ret) {
          _this.getRelationshipList();
          if (ret.data.code == 0) {
            _this.$notify({
              title: ret.data.msg,
              message: '😀',
              type: 'success'
            });
          } else {
            _this.$notify.error({
              title: ret.data.msg,
              message: '😭',
              type: 'error'
            });
          }
        });
      }
    }, created() {
      this.getRouterData(); //获取路由传递的参数
      this.getRelationshipList(); //获取联系人数据
    }
  }
</script>

<style scoped>
  .CustomerContact {
    padding: 30px 30px;
    margin: 30px auto;
  }

  .locationButton {
    float: left;
    margin-top: -30px;
  }

  .kehubianhao {
    margin-top: 80px;
    margin-bottom: 20px;


  }

  .el-form-item {
    margin-top: 0px;
  }

  .input-item {
    width: 305.4px;
  }

  .pager {
    text-align: center;
    margin-top: 10px;
  }

  .el-input,
  .el-select {
    width: 260px;
  }
</style>